// import { JSEncrypt } from 'encryptlong' // ! RSA加密
import { Base64 } from 'js-base64' // ! base64加密
import { JSEncrypt } from 'nodejs-jsencrypt' // ! RSA加密

// import platformConfig from '../../config/PlatformConfig'
// ! 拿到的RSA 公鑰 請先手動轉成base64 讓網頁build後的 js找不到rsa公鑰
// const baseSixFour = platformConfig.baseSixFour
/** 加密
 * @param {object || string} data - 待加密的參數
 * @param {string} method - get or post
 * 流程為 拿 baseSixFour 做Base64 解密 > RSA加密 > get的話就 encodeURI post 則不用
 */
export const encryptedData = async (data, method, baseSixFour) => {
  const encryptor = new JSEncrypt() // 新建JSEncrypt对象
  const decode = await Base64.decode(baseSixFour) // 解密base64 Key
  await encryptor.setPublicKey(decode) // 設置公鑰

  if (method === 'post') {
    const encryptorTemp = await encryptor.encryptLong(data)
    return { data: encryptorTemp }
  }
}
/** ---------------------------------------分段加密開始---------------------------------------------- */
JSEncrypt.prototype.encryptLong = function (string) {
  var k = this.getKey()
  try {
    // RSA每次加密117bytes，需要辅助方法判断字符串截取位置
    // 1.获取字符串截取点
    var bytes = []
    bytes.push(0)
    var byteNo = 0
    var len, c
    len = string.length
    var temp = 0
    for (var i = 0; i < len; i++) {
      c = string.charCodeAt(i)
      if (c >= 0x010000 && c <= 0x10ffff) {
        byteNo += 4
      } else if (c >= 0x000800 && c <= 0x00ffff) {
        byteNo += 3
      } else if (c >= 0x000080 && c <= 0x0007ff) {
        byteNo += 2
      } else {
        byteNo += 1
      }
      if (byteNo % 117 >= 50 || byteNo % 117 === 0) {
        if (byteNo - temp >= 50) {
          bytes.push(i)
          temp = byteNo
        }
      }
    }

    // 2.截取字符串并分段加密
    const dataArr = []
    if (bytes.length > 1) {
      bytes.forEach((item, index) => {
        let str = ''
        if (index === 0) {
          str = string.substring(0, bytes[index + 1] + 1)
          dataArr.push(str)
        } else if (index === bytes.length - 1) {
          const lastStr = string.substring(bytes[index] + 1)
          dataArr.push(lastStr)
        } else {
          str = string.substring(item + 1, bytes[index + 1] + 1)
          dataArr.push(str)
        }
      })
      // 2-2將分段的明文加密
      const newArr = dataArr.map((item) => {
        const base64Data = Base64.encodeURI(item)
        const t = k.encrypt(base64Data)
        return hex2b64(t)
      })
      return newArr
    }

    // 不用分段的明文直接加密
    const base64Data = Base64.encodeURI(string)
    const t = k.encrypt(base64Data)
    const y = hex2b64(t)
    dataArr.push(y)
    return dataArr
  } catch (ex) {
    return false
  }
}

function hex2b64(h) {
  var b64map = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
  var b64padchar = '='
  var i
  var c
  var ret = ''
  for (i = 0; i + 3 <= h.length; i += 3) {
    c = parseInt(h.substring(i, i + 3), 16)
    ret += b64map.charAt(c >> 6) + b64map.charAt(c & 63)
  }
  if (i + 1 === h.length) {
    c = parseInt(h.substring(i, i + 1), 16)
    ret += b64map.charAt(c << 2)
  } else if (i + 2 === h.length) {
    c = parseInt(h.substring(i, i + 2), 16)
    ret += b64map.charAt(c >> 2) + b64map.charAt((c & 3) << 4)
  }
  while ((ret.length & 3) > 0) ret += b64padchar
  return ret
}
/** ---------------------------------------分段加密結束---------------------------------------------- */

export default encryptedData
